package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbean.module.ProducePlan;
import com.tyqx.hashchainbiz.service.ProducePlanService;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchaindao.mapper.ProducePlanMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("producePlanService")
public class ProducePlanServiceImpl implements ProducePlanService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private ProducePlanMapper producePlanMapper;

    @Async
    public Integer producePlan(ProducePlan producePlan) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        producePlan.setCreateTime(createTime);
        producePlan.setUpdateTime(createTime);
        String producePlanStr= producePlan.toString();
        HashIndex hashIndex= HashValueUtil.getHashValue(producePlanStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        producePlan.setHashValue(hashIndex.getHashValue());

        return producePlanMapper.addProducePlan(producePlan);
    }

    @Override
    public List<ProducePlan> getProducePlanById(String donateId, String userName) {
        return producePlanMapper.getProducePlanById(donateId);
    }

    @Override
    public List<ProducePlan> getProducePlanByTime(Date startTime, Date endTime, String userName) {
        return producePlanMapper.getProducePlanByTime(startTime, endTime, userName);
    }
}
